using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Glpt.Common
{
    /// <summary>
    /// DES加密类
    /// </summary>
    public class DESCrypto
    {
        /// <summary>
        /// 字符编码
        /// </summary>
        public Encoding Coding = null;
        /// <summary>
        /// Key
        /// </summary>
        public byte[] Key = null;
        /// <summary>
        /// IV
        /// </summary>
        public byte[] IV = null;
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public DESCrypto()
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            Key = des.Key;
            IV = des.IV;
            Coding = new UnicodeEncoding();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="iv">IV</param>
        public void Init(string key, string iv)
        {
            Key = Encoding.ASCII.GetBytes(key);
            IV = Encoding.ASCII.GetBytes(iv);
        }

        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="txt">要加密的字符串</param>
        /// <returns>加密后字符串</returns>
        public String Encrypt(String txt)
        {
            byte[] bArray = Coding.GetBytes(txt);
            byte[] CypherTextBArray = Encrypt(bArray);
            return Convert.ToBase64String(CypherTextBArray);
        }

        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="bArray">要加密的Byte数组</param>
        /// <returns>加密后的Byte数组</returns>
        public byte[] Encrypt(byte[] bArray)
        {
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV), CryptoStreamMode.Write);
            cStream.Write(bArray, 0, bArray.Length);
            cStream.FlushFinalBlock();
            byte[] ret = mStream.ToArray();
            cStream.Close();
            mStream.Close();
            return ret;
        }

        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="txt">要解密的字符串</param>
        /// <returns>解密后字符串</returns>
        public String Decrypt(String txt)
        {
            byte[] bArray = Convert.FromBase64String(txt);
            byte[] DypherTextBArray = Decrypt(bArray);
            string val = Coding.GetString(DypherTextBArray);
            int idx = val.IndexOf('\0');
            if (idx >= 0)
                val = val.Substring(0, idx);
            return val;
        }

        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="bArray">要解密的字符串</param>
        /// <returns>解密后字符串</returns>
        public byte[] Decrypt(byte[] bArray)
        {
            if (bArray.Length == 0) return bArray;
            MemoryStream msDecrypt = new MemoryStream(bArray);
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV), CryptoStreamMode.Read);
            byte[] DecryptDataArray = new byte[bArray.Length];
            csDecrypt.Read(DecryptDataArray, 0, DecryptDataArray.Length);
            msDecrypt.Close();
            csDecrypt.Close();
            return DecryptDataArray;
        }
    }
}
